﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using DICE_GS;

public enum TABLENAME
{
    SKILL_DATA,
    UNIT_DATA,
    ITEM_DATA,
    TILE_DATA,
    GENERALSETUP_DATA
};

public class Tables
{
	public static readonly string WEB_SERVER_URL = "http://172.20.80.159:8080/dice_deploy";

	public static readonly string WEB_DOWNLOAD_URL = "http://172.20.80.159:8080/res/";

    static Tables singleton;

    public static string Skill_data = "";
    public static string Unit_data = "";
    public static string Item_data = "";
    public static string Tile_data = "";
    public static string GeneralSetup_data = "";

    public static Hashtable dataFileList = new Hashtable();

    public static bool isLoadDone = false;

    public static void LoadStaticTable()
    {
        if (singleton == null) singleton = new Tables();

        foreach (string key in dataFileList.Keys)
        {
            string tableBody = singleton.LoadTableFromCache(key);

            switch (key)
            {
                case "GeneralSetup_data.xml":
                    StageManagerWrapper.Instance.LoadGeneralSetupData(tableBody, true);
                    break;
                case "StringTable_data.xml":
                    StageManagerWrapper.Instance.LoadStringTableData(tableBody, true);
                    break;
                case "StringTable_Eng_data.xml":
                    break;
                case "StringTable_General_data.xml":
                    break;
            }
        }
    }

    public static void LoadTablesForOutGame()
    {
        if (singleton == null) singleton = new Tables();

        foreach (string key in dataFileList.Keys)
        {
            string tableBody = singleton.LoadTableFromCache(key);

            switch (key)
            {
                case "ItemDropProb_data_drop_prob.xml":
                case "ItemDropProb_data_event_drop_group.xml":
                    break;
                case "ItemShop_data.xml":
                    StageManagerWrapper.Instance.LoadShopItemData(tableBody, true);
                    break;
                case "Map_data.xml":
                    StageManagerWrapper.Instance.LoadMapData(tableBody, true);
                    break;
                case "Tile_data.xml":
                    StageManagerWrapper.Instance.LoadBoardData(tableBody, true);
                    break;
                case "Unit_Ability_data.xml":
                    StageManagerWrapper.Instance.LoadUnitAbilityData(tableBody, true);
                    break;
                case "Unit_Exp_data.xml":
                    StageManagerWrapper.Instance.LoadUnitExpData(tableBody, true);
                    break;
                case "Unit_CombineCost_data.xml":
                    StageManagerWrapper.Instance.LoadUnitCombineCostData(tableBody, true);
                    break;
                case "EventScheduler_data_attendance_reward.xml":
                    //StageManagerWrapper.Instance.LoadAttendanceRewardData(tableBody, true);
                    break;
                case "MapChapter_data.xml":
                    StageManagerWrapper.Instance.LoadPVEStageData(tableBody, true);
                    break;
            }
        }
    }

    public static void UnLoadTablesForInGame()
    {
        CAttendanceDataManager.Instance.UnLoadXMLData();
        CPVEStageDataManager.Instance.UnLoadXMLData();
        CShopItemDataManager.Instance.UnLoadXMLData();
        CUnitAbilityDataManager.Instance.UnLoadXMLData();
        CUnitCombineCostDataManager.Instance.UnLoadXMLData();
        CUnitExpDataManager.Instance.UnLoadXMLData();
    }

    public static void LoadTable()
    {
		if( !isLoadDone )
		{
	        if (singleton == null) singleton = new Tables();
	
	        foreach (string key in dataFileList.Keys)
	        {
				string tempKey=key;
				if( !key.Contains(".xml") )
					tempKey += ".xml";

	            string tableBody = singleton.LoadTableFromCache(tempKey);

				switch (tempKey)
	            {
					case "Skill_Data.xml":
						StageManagerWrapper.Instance.LoadSkillData(tableBody, true);
	                    break;
	                case "GeneralSetup_data.xml":
	                    StageManagerWrapper.Instance.LoadGeneralSetupData(tableBody, true);
	                    break;
					case "ItemDropProb_data_drop_prob.xml":
					case "ItemDropProb_data_event_drop_group.xml":
	                    break;
					case "ItemShop_data.xml":
	                    StageManagerWrapper.Instance.LoadShopItemData(tableBody, true);
	                    break;
					case "Map_data.xml":
	                    StageManagerWrapper.Instance.LoadMapData(tableBody, true);
	                    break;
					case "StringTable_data.xml":
	                    StageManagerWrapper.Instance.LoadStringTableData(tableBody, true);
	                    break;
					case "StringTable_Eng_data.xml":
	                    break;
					case "StringTable_General_data.xml":
	                    break;
					case "Tile_data.xml":
	                    StageManagerWrapper.Instance.LoadBoardData(tableBody, true);
	                    break;
					case "Unit_Ability_data.xml":
	                    StageManagerWrapper.Instance.LoadUnitAbilityData(tableBody, true);
	                    break;
					case "Unit_Exp_data.xml":
	                    StageManagerWrapper.Instance.LoadUnitExpData(tableBody, true);
	                    break;
					case "Unit_Data.xml":
	                    StageManagerWrapper.Instance.LoadUnitData(tableBody, true);
	                    break;
					case "Unit_Preset_data.xml":
						StageManagerWrapper.Instance.LoadUnitPresetData(tableBody, true);
						break;
					case "Item_data.xml":
	                    StageManagerWrapper.Instance.LoadItemData(tableBody, true);
	                    break;
					case "Dice_data.xml":
						StageManagerWrapper.Instance.LoadDiceData(tableBody, true);
						break;
                    case "Unit_CombineCost_data.xml":
                        StageManagerWrapper.Instance.LoadUnitCombineCostData(tableBody, true);
                        break;
                    case "EmotionData_AI_data.xml":
                        StageManagerWrapper.Instance.LoadAIEmotionData(tableBody, true);
                        break;
                    case "EmotionData_data.xml":
                        StageManagerWrapper.Instance.LoadEmotionData(tableBody, true);
                        break;
                    case "Achievement_data.xml":
                        StageManagerWrapper.Instance.LoadAchievementData(tableBody, true);
                        break;
                    case "EventScheduler_data_attendance_reward.xml":
                        //StageManagerWrapper.Instance.LoadAttendanceRewardData(tableBody, true);
                        break;
                    case "MapChapter_data.xml":
                        StageManagerWrapper.Instance.LoadPVEStageData(tableBody, true);
                        break;
					case "Character_data.xml":
						StageManagerWrapper.Instance.LoadCharacterData(tableBody, true);
						break;
                    case "PlayerLevelBonus_data.xml":                        
                        StageManagerWrapper.Instance.LoadPlayerLevelBonusData(tableBody, true);
                        break;  
                    case "Recipe_data.xml":
                        StageManagerWrapper.Instance.LoadRecipeData(tableBody, true);
                        break;
				}
			}
		}
		
		isLoadDone = true;
        Debug.Log("Tables.isLoadDone : " + isLoadDone);
    }

    public string LoadTableFromCache(string fileName)
    {
        string FN = string.Format("{0}/UCUpdater/{1}", SystemCore.GetAssetPath(), fileName);
        //Debug.LogError(FN);
        return LCFileManager.FileReadAllText(FN);
    }

    string GetTableFileName(TABLENAME name)
    {
        switch (name)
        {
            case TABLENAME.SKILL_DATA:
                return "Skill_data.xml";
            case TABLENAME.UNIT_DATA:
                return "Unit_data.xml";
            case TABLENAME.ITEM_DATA:
                return "Item_data.xml";
            case TABLENAME.TILE_DATA:
                return "Tile_data.xml";
            case TABLENAME.GENERALSETUP_DATA:
                return "GeneralSetup_data.xml";
        }

        return null;
    }
}

 